﻿using System;
using csctp;


class Program
{
    static void Main(string[] args)
    {
        // See https://aka.ms/new-console-template for more information
        Console.WriteLine("Hello, World!");
        TestQuote tq = new TestQuote();
        tq.Run();
        TestTrade tt = new TestTrade();
        tt.Run();
        Console.ReadKey();
    }
}

class TestTrade
{
    CTPTrade t = new CTPTrade();

    public void Run()
    {
        t.OnFrontConnected += OnFrontConnected;
        t.OnRspUserLogin += OnRspUserLogin;
        t.OnRtnOrder += OnRtnOrder;
        t.OnRtnTrade += OnRtnTrade;
        t.OnRtnCancel += OnRtnCancel;
        t.OnRtnErrCancel += OnRtnErrCancel;
        t.OnRtnError += OnRtnError;
        t.OnRtnErrOrder += OnRtnErrOrder;
        t.OnRtnExchangeStatus += OnRtnExchangeStatus;
        t.OnRtnNotice += OnRtnNotice;
        t.OnRtnPasswordUpdate += OnRtnPasswordUpdate;

        Console.WriteLine(t.Version);
        t.ReqConnect("tcp://180.168.146.187:10130");
    }

    private void OnRtnPasswordUpdate(object sender, ErrorEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void OnRtnNotice(object sender, StringEventArgs e)
    {
        Console.WriteLine(e.Value);
    }

    private void OnRtnExchangeStatus(object sender, StatusEventArgs e)
    {
        Console.WriteLine("OnRtnExchangeStatus {0}:{1}", e.Exchange, e.Status);
    }

    private void OnRtnErrOrder(object sender, ErrOrderArgs e)
    {
        Console.WriteLine("OnRtnErrOrder {0}:{1}", e.ErrorID, e.ErrorMsg);
    }

    private void OnRtnError(object sender, ErrorEventArgs e)
    {
        Console.WriteLine("OnRtnError {0}:{1}", e.ErrorID, e.ErrorMsg);
    }

    private void OnRtnErrCancel(object sender, ErrOrderArgs e)
    {
        Console.WriteLine("OnRtnErrCancel {0}:{1}", e.ErrorID, e.ErrorMsg);
    }

    private void OnRtnCancel(object sender, OrderArgs e)
    {
        Console.WriteLine(e.Value.InsertTime);
    }

    private void OnRtnTrade(object sender, TradeArgs e)
    {
        Console.WriteLine("OnRtnTrade {0}", e.Value);
    }

    private void OnRtnOrder(object sender, OrderArgs e)
    {
        Console.WriteLine("OnRtnOrder {0}", e.Value);
    }

    private void OnRspUserLogin(object sender, ErrorEventArgs e)
    {
        Console.WriteLine("OnRspUserLogin {0}:{1}", e.ErrorID, e.ErrorMsg);
        ((CTPTrade)sender).ReqOrderInsert("rb2301", DirectionType.Buy, OffsetType.Open, 3800, 2, 3333);
    }

    private void OnFrontConnected(object sender, EventArgs e)
    {
        Console.WriteLine("trade connected.");
        ((CTPTrade)sender).ReqUserLogin("9999", "008107", "1", "simnow_client_test", "0000000000000000");
    }
}

class TestQuote
{
    CTPQuote q = new CTPQuote();
    public void Run()
    {
        q.OnFrontConnected += onConnect;
        q.OnRspUserLogin += onLogin; ;
        q.OnFrontDisconnected += onDisconnect;
        q.OnRtnTick += ontick;

         //q.ReqConnect("tcp://180.168.146.187:10212");
        q.ReqConnect("tcp://180.168.146.187:10131");
        Console.ReadKey();
    }

    private void onLogin(object sender, ErrorEventArgs e)
    {
        Console.WriteLine("{0}:{1}", e.ErrorID, e.ErrorMsg);
        ((CTPQuote)sender).ReqSubscribeMarketData("rb2301");
    }

    void onDisconnect(object sender, IntEventArgs e)
    {
        Console.WriteLine("disconnected: ", e.Value);
    }


    void onConnect(object sender, EventArgs e)
    {
        Console.WriteLine("quote connected.");
        ((CTPQuote)sender).ReqUserLogin("9999", "008105", "1");
    }

    void ontick(object sender, TickEventArgs e)
    {
        Console.WriteLine("{0},{1},{2}", e.Tick.UpdateTime, e.Tick.InstrumentID, e.Tick.LastPrice);
    }
}